﻿using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using iMobileS.Common;
using iMobileS.Models;
using iMobileS.Models.Partial;
using System.Linq;
namespace iMobileS.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetCategories()
        {
            var lstCategories = Services.Get<ICategoriesRepository>().GetCategories();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return Content(serializer.Serialize(lstCategories));
        }

        public ActionResult GetProductsByCategory(int cateId)
        {
            var lstProducts = Services.Get<ICategoriesRepository>().GetProductsByCategory(cateId);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return Content(serializer.Serialize(lstProducts));
        }

        public ActionResult GetRegions()
        {
            var lstRegions = Services.Get<ICategoriesRepository>().GetRegions();
            lstRegions.Insert(0, new Regions() {RegionID = 0, RegionDescription = "Select"});
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return Content(serializer.Serialize(lstRegions));
        }
        
        public ActionResult GetShippers()
        {
            var lstShippers = Services.Get<ICategoriesRepository>().GetShippers();
            lstShippers.Insert(0, new Shippers() {ShipperID  = 0, CompanyName = "Select" });
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return Content(serializer.Serialize(lstShippers));
        }

        public ActionResult GetEmployees()
        {
            var lstEmployees = Services.Get<ICategoriesRepository>().GetEmployees();
            lstEmployees.Insert(0, new Employees() { EmployeeID = 0, LastName = "Select" });
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return Content(serializer.Serialize(lstEmployees));
        }

        public ActionResult GetCustomers()
        {
            var lstCustomers = Services.Get<ICategoriesRepository>().GetCustomers();
            lstCustomers.Insert(0, new Customers() {CustomerID = string.Empty, CompanyName = "Select"});
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return Content(serializer.Serialize(lstCustomers));
        }

        public ActionResult InsertOrder(string orders, string orderdetails)
        {
            var results = true;
            Orders objOrders = JsonToObject<Orders>.ToObject(orders);
           List<OrderDetails> lstOrderDetailses= JsonToObject<OrderDetails>.ToList(orderdetails);
           results = Services.Get<ICategoriesRepository>().InsertOrder(objOrders, lstOrderDetailses);
            return Content("{'success': '" + results + "'}");
        }
    }
}
